import { Body, Controller, Post } from "@nestjs/common";
import { ApiBearerAuth, ApiBody, ApiOperation, ApiTags } from "@nestjs/swagger";
import { ModuleSwaggerTags } from "~/constants/modules.constant";
import { LogService } from "~/modules/log/log.service";
import { ArticleLogDto, SurveyLogDto } from "~/modules/log/log.dto";
import { AuthUser } from "~/modules/auth/decorators/auth-user.decorator";
import { AllowAnon } from "~/modules/auth/decorators/allow-anon.decorator";
import { Public } from "~/modules/auth/decorators/public.decorator";

@ApiTags(ModuleSwaggerTags.LOG)
@ApiBearerAuth()
@Controller('cms/log')
export class MiniappLogController {
	constructor(private readonly logService: LogService) {}

	@Public()
	@Post('article')
	@ApiOperation({ summary: '保存文章阅读日志' })
	@ApiBody({ type: ArticleLogDto })
	async saveArticleLog(
		@Body() dto: ArticleLogDto,
		@AuthUser() user: IAuthUser
	) {
		return await this.logService.saveArticleLog(
			dto.articleId,
			user?.uid ?? null,
			dto.articleType,
			dto.readDuration,
			dto.categoryId
		);
	}

	@Public()
	@Post('survey')
	@ApiOperation({ summary: '保存问卷调查日志' })
	@ApiBody({ type: SurveyLogDto })
	async saveSurveyLog(
		@Body() dto: SurveyLogDto,
		@AuthUser() user: IAuthUser
	) {
		return await this.logService.saveSurveyLog(
			dto.surveyId,
			user?.uid ?? null,
			dto.isFinish,
			dto.duration,
			dto.score,
			dto.answers,
			dto.surveyType
		);
	}
}